define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Refund, $window, $timeout, $ionicSlideBoxDelegate, GoPage) {
		var status = new Array();
		var intervene = new Array();
		$scope.refunding_data = new Array();
		$scope.decline_data = new Array();
		$scope.refunded_data = new Array();
		$scope.isLoad = false; //可以加载
		
		$scope.data = '';
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			//动态设置高度
			var all = $window.innerHeight;
			//计算滚动条
			$scope.myObj = {
				"height": all - 44 + "px"
			};
		}, false);

		//滑动时候触发
		$scope.selectIndex = 0;
		$scope.slideChanged = function(index) {
			$scope.selectIndex = index;
		};
		$scope.activeSlide = function(index) {
			$scope.selectIndex = index;
			$ionicSlideBoxDelegate.$getByHandle('main-handle').slide(index);
		};
		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			$timeout(function() {
				$scope.$broadcast('scroll.refreshComplete');
			}, 1000);
		};
		//加载更多
		$scope.doRefreshMore = function() {
			$timeout(function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
			}, 1000);
		};

		//页面跳转
		$scope.onshow_detail = function(order_id) {
			GoPage.go_states("refundtenderdetail",{order_id:order_id});
		}
		$scope.init_data = function(data) {
			Refund.refund_list(1, function(data) {
				$scope.money = data.order;
//				$scope.data = $scope.datas;
				$scope.trans();
//				$scope.get_refun();
				$scope.get_decline();
				$scope.get_refunded();
				$scope.get_refunding();
			})
			Refund.refund_list(2, function(data) {
				$scope.goods = data.order;
//				$scope.data = $scope.datas;
				$scope.trans();
//				$scope.get_refun();
				$scope.get_decline();
				$scope.get_refunded();
				$scope.get_refunding();
			})
		}
		$scope.init_status = function() {
			status = new Array(17);
			status[0] = "";
			status[1] = "退款协议等待卖家确认";
			status[2] = "审核中";
			status[3] = "同意退款";
			status[4] = "完成";
			status[5] = "拒绝";
			status[6] = "已收货";
			status[7] = "已质检";
			status[8] = "补差价";
			status[9] = "已拒绝退款";
			status[10] = "已取消";
			status[11] = "卖家不同意协议，等待买家修改";
			status[12] = "买家已退货，等待卖家确认收货";
			status[13] = "已修改";
			status[14] = "卖家收到退货，拒绝退款";
			status[15] = "卖家同意退款，等待卖家打款至平台";
			status[16] = "卖家已退款，等待系统结算";
		}
		$scope.init_intervene = function() {
			intervene = new Array(5);
			intervene[0] = '';
			intervene[1] = '平台未介入';
			intervene[2] = '等待卖家举证';
			intervene[3] = '平台已介入';
			intervene[4] = '平台已处理';
		}
		$scope.trans = function() {
			$scope.init_status();
			$scope.init_intervene();
			for(var i = 0; i < $scope.datas.length; i++) {
				$scope.datas[i].statu = status[$scope.datas[i].status];
				$scope.datas[i].intervene = intervene[$scope.datas[i].is_intervene];
				//				$scope.datas[i].renfun_statu = 
			}
			console.log(status);
			console.log($scope.datas);

		}
		
		$scope.get_refunding = function() {
			for(var i = 0; i < $scope.datas.length; i++) {
				if($scope.datas[i].refund_status == 1) {
					var n = $scope.refunding_data.length;
					$scope.refunding_data[n] = $scope.datas[i];
				}
			}
			console.log($scope.refunding_data);
		}
		$scope.get_refunded = function() {
			for(var i = 0; i < $scope.datas.length; i++) {
				console.log($scope.datas[i].refund_status);
				if($scope.datas[i].refund_status == 4 || $scope.datas[i].refund_status == 3) {
					var n = $scope.refunded_data.length;
					$scope.refunded_data[n] = $scope.datas[i];
				}
			}
			console.log($scope.refunded_data);
			
		}
		$scope.get_decline = function() {
			for(var i = 0; i < $scope.datas.length; i++) {
				if($scope.datas[i].refund_status == 2) {
					var n = $scope.decline_data.length;
					$scope.decline_data[n] = $scope.datas[i];
				}
			}
			console.log($scope.decline_data);
		}
		$scope.show = function(type) {
			console.log(type);
			if(type == 'all') {
				
				$scope.data = $scope.datas;
			} else if(type == 'refunding') {
				
				$scope.data = $scope.refunding_data;
			} else if(type == 'refunded') {
				
				$scope.data = $scope.refunded_data;
			} else if(type == 'decline') {
				
				$scope.data = $scope.decline_data;
			}
			console.log($scope.data);
		}
		$scope.return_send = function(order_id,comp_id,ship_no){
			Refund.return_send(order_id,comp_id,ship_no,'type',function(data){
				
			})
		}
		$scope.init_data();
		$scope.show_buyerpayment = function(store_id){
			GoPage.go_states("buyerpayment",{"store_id":store_id});
		}

	}

	controller.$inject = ['$scope', 'Refund', '$window', '$timeout', '$ionicSlideBoxDelegate', 'GoPage'];

	return app.registerController("refundctrl",controller);
});